package com.zha.boss.mvc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zha.boss.PowerBossApplication;
import com.zha.boss.dao.entity.PayWay;
import com.zha.boss.dao.mapper.PayWayMapper;
import com.zha.boss.dao.result.DicItem;
import com.zha.boss.enums.resp.RespCode;
import com.zha.boss.enums.status.PublicStatus;
import com.zha.boss.exceptions.BossException;
import com.zha.boss.mvc.models.forms.PayWayConfigForm;
import com.zha.boss.mvc.service.PayWayService;
import com.zha.boss.util.NoBuilderUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zha
 * @CreateTime: 2024年09月15日 13:45
 */
@Service
public class PayWayServiceImpl implements PayWayService {
    @Resource
    private PayWayMapper payWayMapper;
    @Override
    public IPage<PayWay> pageList(String productCode, Integer pageNo, Integer pageSize) {
        QueryWrapper<PayWay> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("pay_product_code","way_code","type_code");

        if(StrUtil.isNotBlank(productCode)){
            queryWrapper.eq("pay_product_code",productCode.trim());
        }
        return payWayMapper.selectPage(Page.of(pageNo, pageSize), queryWrapper);
    }

    @Override
    public List<DicItem> queryPayWayDicList() {
        return PowerBossApplication.DIC_PAYWAY_LIST;
    }

    @Override
    public List<DicItem> queryPayTypeDicList(String wayCode) {
        List<DicItem> dicItemList = PowerBossApplication.DIC_TYPE_MAP.get(wayCode.toUpperCase());
        return dicItemList == null? new ArrayList<>() : dicItemList;
    }

    @Override
    public void addPayWayConfig(PayWayConfigForm payWayConfigForm) {
        // 1. productCode，wayCode, typeCode 三者必须唯一
        QueryWrapper<PayWay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_product_code", payWayConfigForm.getProductCode())
                    .eq("way_code", payWayConfigForm.getWayCode())
                    .eq("type_code", payWayConfigForm.getTypeCode());
        PayWay payWay = payWayMapper.selectOne(queryWrapper);
        if (payWay != null){
            throw new BossException(RespCode.PAY_WAY_CONFIG_EXISTS);
        }
        // 2. 添加支付方式配置
        payWay = new PayWay();
        payWay.setId(NoBuilderUtil.getUUID());
        payWay.setWayCode(payWayConfigForm.getWayCode());
        payWay.setWayName(PowerBossApplication.PAY_WAY_NAME_MAP.get(payWayConfigForm.getWayCode()));
        payWay.setTypeCode(payWayConfigForm.getTypeCode());
        String typeNameKey = payWayConfigForm.getWayCode()+":"+payWayConfigForm.getTypeCode();
        payWay.setTypeName(PowerBossApplication.PAY_TYPE_NAME_MAP.get(typeNameKey));
        payWay.setPayProductCode(payWayConfigForm.getProductCode());
        payWay.setPayRate(payWayConfigForm.getPayRate());
        payWay.setSorts(payWayConfigForm.getSorts());
        payWay.setStatus(PublicStatus.ENABLE.name());
        payWay.setEditTime(new Date());
        payWay.setRemark("创建支付方式配置");
        payWayMapper.insert(payWay);

    }

    /**
     * 删除支付方式配置
     */
    @Override
    public void removePayWay(String id) {
       payWayMapper.deleteById(id);
    }
}
